package com.sdcqjy.mylibrary.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;


/**
 * Created by Administrator on 2017/5/17.
 * 用于继承的父Adapter 超adapter
 */
public abstract class SuperRecyclerAdapter<T> extends RecyclerView.Adapter<SuperRecyclerAdapter.ViewHolder> {
    public SuperRecyclerAdapter(Context context, List<T> modeList) {
        this.context = context;
        this.modeList = modeList;
        inflater = LayoutInflater.from(context);
    }

    public abstract int getLayoutId();

    public abstract ViewHolder<T> getViewHolder(View view);

    public ViewHolder<T> onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = inflater.inflate(getLayoutId(), parent, false);
        return getViewHolder(inflate);
    }

    public void onBindViewHolder(ViewHolder holder, int position) {
        if (position < modeList.size()) holder.setData(modeList.get(position));
        else holder.setData(null);
    }

    public int getItemCount() {
        return modeList.size();
    }

    protected List<T> modeList;
    protected Context context;
    protected LayoutInflater inflater;


    public static class ViewHolder<T> extends RecyclerView.ViewHolder {
        protected T mode;

        public ViewHolder(View view) {
            super(view);
        }

        public void setData(T mode) {
            this.mode = mode;
        }
    }
}
